package com.yueqian.teachercode;

import java.io.*;

public class TestCharsIO {

    public static void main(String[] args) {
        //写字符
        writeChars("fileDir/bb.txt", "I love this game\r\nnext line\r\n");
        //读字符
        String str = readChars("fileDir/bb.txt");
        System.out.println(str);

    }

    /**
     * 读字符
     */
    private static String readChars(String filePath) {
        String result = null;
        //过滤非法
        if (filePath == null || filePath.isEmpty()) {
            System.out.println("ERROR: 输入非法");
            return null;
        }
        //封装文件
        File afile = new File(filePath);
        //是否存在
        if (!afile.isFile()) {
            System.out.println("ERROR: 文件不存在");
            return null;
        }
        //字符数组
        char[] chars = null;
        FileInputStream fin = null;
        Reader reader = null;
        try {
            //文件输入流关联物理文件
            fin = new FileInputStream(afile);
            //字符流，关联文件输入通道
            reader = new InputStreamReader(fin);
            //根据内容封装字符数组大小
            chars = new char[fin.available()];
            //读取内容
            reader.read(chars);
            //将字符数组转换为String
            result = new String(chars);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                if (fin != null) {
                    fin.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }


        return result;
    }

    /**
     * 写文件
     */
    private static void writeChars(String filePath, String content) {
        if (filePath == null || filePath.isEmpty() || content == null || content.isEmpty()) {
            System.out.println("Error:输入有误");
            return;
        }
        //封装文件
        File afile = new File(filePath);
        //获取路径
        File dir = afile.getParentFile();
        if (!dir.isDirectory()) {
            dir.mkdirs();
        }

        //文件输出流
        FileOutputStream fout = null;
        //字符输出流
        Writer writer = null;
        try {
            //文件输出通道（流）关联物理文件
            fout = new FileOutputStream(afile, true);
            //字符的输出通道关联文件通道
            writer = new OutputStreamWriter(fout);
            //写字符到文件
            writer.write(content);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (writer != null) {
                    writer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fout != null) {
                    fout.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }

}
